package com.jsyh.maidu.fragment;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.ArticleActivity;
import com.jsyh.maidu.activity.MainActivity;
import com.jsyh.maidu.activity.SearchActivity;
import com.jsyh.maidu.activity.ShoppingCartActivity;
import com.jsyh.maidu.adapter.home.HomeAdapter;
import com.jsyh.maidu.model.BestNewHot;
import com.jsyh.maidu.model.HomePageData;
import com.jsyh.maidu.model.HomePageModel;
import com.jsyh.maidu.model.HomePageMoreModel;
import com.jsyh.maidu.presenter.HomePageMorePresenter;
import com.jsyh.maidu.presenter.HomePagePresenter;
import com.jsyh.maidu.qrzxing.CaptureActivity;
import com.jsyh.maidu.views.FeedRootRecyclerView;
import com.jsyh.maidu.views.HomePageMoreView;
import com.jsyh.maidu.views.HomePageView;
import com.umeng.socialize.facebook.controller.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/6/7.
 */

public class HomeFragment extends BaseFragment implements HomePageView, HomePageMoreView{

    private View rootView;
    private FeedRootRecyclerView mRecyclerView;
    private SwipeRefreshLayout refreshLayout;

    private List<Integer> typeList = new ArrayList<>();

    private HomePageData homePageData;
    private LinearLayout search;

    private HomePageMorePresenter homePageMorePresenter;

    private LinearLayoutManager layoutManager;

    private boolean isFreshing;

    private List<BestNewHot> moreList;

    private HomeAdapter adapter;

    private RecyclerView.OnScrollListener scrollListener;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        for (int i = 1; i < 7; i++) {
            typeList.add(i);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_home, container, false);
        }

        return rootView;
    }

    @Override
    protected void initView() {

        mRecyclerView = (FeedRootRecyclerView) rootView.findViewById(R.id.recyclerView);
        rootView.findViewById(R.id.txtGoCar).setOnClickListener(this);
        rootView.findViewById(R.id.txtMessage).setOnClickListener(this);
        search = (LinearLayout) rootView.findViewById(R.id.lineLaySearch);
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.home_page_refresh);

        homePageMorePresenter = new HomePageMorePresenter(this);

        search.setOnClickListener(this);
        layoutManager = new LinearLayoutManager(context);
        homePageMorePresenter = new HomePageMorePresenter(this);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (isFreshing){
                    return;
                }else {
                    refreshLayout.setRefreshing(true);
                    isFreshing = true;
                    homePageMorePresenter.setPageCount(1);
                    initData();
                }
            }
        });
        scrollListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                boolean isBootom = recyclerView.canScrollVertically(1);
                if (!isBootom) {
                    int page = homePageMorePresenter.getPageCount();
                    homePageMorePresenter.setPageCount(page + 1);
                    homePageMorePresenter.getMoreHomePage(getActivity());
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }
        };

    }

    @Override
    protected void initData() {
        HomePagePresenter presenter = new HomePagePresenter(this);
        presenter.getHomePageData(context);
    }



    @Override
    protected void initTitle() {

    }

    @Override
    public void getData(HomePageModel response) {
        if (response.getCode().equals("1")){
            refreshLayout.setRefreshing(false);
            isFreshing = false;
            homePageData = response.getData();
            adapter = new HomeAdapter(context, typeList, homePageData, (MainActivity) getActivity());
            mRecyclerView.setLayoutManager(layoutManager);
            mRecyclerView.setAdapter(adapter);
        }else {
            Log.d("HOMEFRAGMENT", "response: ");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mRecyclerView.addOnScrollListener(scrollListener);
    }

    @Override
    public void onPause() {
        super.onPause();
        mRecyclerView.removeOnScrollListener(scrollListener);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.txtGoCar:
                Intent carintent =new Intent(getActivity(),ShoppingCartActivity.class);
                startActivity(carintent);
                break;

            case R.id.txtMessage:
                Intent intent = new Intent(getActivity(), ArticleActivity.class);
                startActivity(intent);
                break;

            case R.id.lineLaySearch:
                Intent search = new Intent(context, SearchActivity.class);
                startActivity(search);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1){
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Intent itCapture = new Intent(getActivity(), CaptureActivity.class);
                startActivity(itCapture);

            }else {
                ToastUtil.showToast(context, "权限被禁止，相关功能无法使用！");
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

    @Override
    public void getMoreData(HomePageMoreModel response) {
        refreshLayout.setRefreshing(false);
        if (response.getCode().equals("1")){
            moreList = response.getData();
            homePageData.getBest().addAll(moreList);
            adapter.notifyDataSetChanged();
        }else if (response.getCode().equals("500")){
            Toast.makeText(getActivity(), "已经到底了！", Toast.LENGTH_SHORT).show();
        }else {
            String msg = response.getMsg().toString();
            if (TextUtils.isEmpty(msg)){

            }else Toast.makeText(getActivity(), response.getMsg().toString(), Toast.LENGTH_SHORT).show();
        }
    }
}
